
################################################################################################
# Figure 5: Allocation of political attention to foreign affairs sub-fields over time, 2001-2014
################################################################################################

# Programme:  cfsp-analysis04a-areaRel.r
# Author:     Frank Haege, Department of Politics and Administration, University of Limerick
# Contact:		frank.haege@ul.ie

# Description
#############
# This script creates Figure 5 of the article. 


# Remove all objects
rm(list = ls(all = TRUE))

# Load libraries
library(data.table)
library(ggplot2)
library(ggpubr)
library(mgcv)

# Set working directory
setwd("C:\\Users\\Frank\\Dropbox\\European Union\\CFSP attention\\EUP submission")

# Set options
options(stringsAsFactors=F)

# Load data
load("Data management\\cfsp-management02-semesterData.RData")

# Remove coordinating working parties
df = df[df$coord!=1, ]
# Coordinating working parties do not belong to any particular sub-area

# Remove all working party variables
df = subset(df, select=-c(wppolicy, nowp, id, wpname, coord, diff.a, dur.lag, diff.r))

# Aggregate by policy sub-area
df = data.table(df)
df = df[, .(dur=sum(dur)), by=c(names(df)[names(df)!="dur"])]

# Calculate share of meetings by semester
df = df[, perc:=prop.table(dur)*100, by="yearsem"]
df = data.frame(df)
df = df[order(df$yearsem, df$comp, df$area), ]

# Loop through all policy areas
area.list = names(table(df$area))
plot.list = list()
for (i in 1:5) {
  j = area.list[[i]]
  dta = df[df$area==j, ]
  
  # Estimate GAM of number of meetings with interaction of time by Lisbon Treaty
  m = gam(data=dta, formula = perc ~ s(time) + s(time, by=lisbonsem) + lisbonsem)
  #print(summary(m))
  
  # Predict fitted values and standard errors
  pv = data.frame(predict(m, se.fit=T, type="response"))
  
  # Create dataframe for fitted values and confidence intervals
  fitted = data.frame(perc=dta$perc, yearsem=dta$yearsem, time=dta$time, lisbonsem=dta$lisbonsem, 
                      pv, area=dta$area)
  fitted$ci.low = fitted$fit-1.96*fitted$se.fit
  fitted$ci.high = fitted$fit+1.96*fitted$se.fit
  
  # Plot duration of meetings over time and overlay predicted values
  p = ggplot() +
    geom_point(data=fitted, aes(x=time, y=perc)) +
    ggtitle(j) +
    geom_line(data=fitted[fitted$lisbonsem==0, ], aes(x=time, y=fit)) +
    geom_line(data=fitted[fitted$lisbonsem==1, ], aes(x=time, y=fit)) +
    geom_ribbon(data=fitted[fitted$lisbonsem==0, ],aes(x=time, ymin=ci.low,ymax=ci.high),alpha=0.3) +
    geom_ribbon(data=fitted[fitted$lisbonsem==1, ],aes(x=time, ymin=ci.low,ymax=ci.high),alpha=0.3) +
    #ylim(0, 250) + 
    ylab("Share of meetings") +
    xlab("Year") +
    geom_vline(xintercept=18.5, linetype="dashed") +
    scale_x_continuous(breaks=c(3, 11, 19, 27), labels=c("2002", "2006", "2010", "2014")) +
    theme_bw(base_size=10) +
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank())
  plot.list[[i]] = p
}
p01 = plot.list[[1]]
p02 = plot.list[[2]]
p03 = plot.list[[3]]
p04 = plot.list[[4]]
p05 = plot.list[[5]]
rm(i, j, m, p, plot.list, area.list, fitted, dta, pv)

# Combine plots
plots = ggarrange(p01, p02, p03, p04, p05,
                  ncol=2, nrow=3)
plots
ggexport(plots, filename="Data analysis\\Figures\\cfsp-analysis04a-areaRel.png", width=500, height=500)
